package com.xgx.distqcp;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.xgx.distqcp.Common.MyMetaBuilder;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.jboot.codegen.CodeGenHelpler;
import io.jboot.codegen.model.JbootBaseModelGenerator;
import io.jboot.codegen.model.JbootModelnfoGenerator;
import io.jboot.config.JbootProperties;
import io.jboot.db.datasource.DatasourceConfig;

import javax.sql.DataSource;
import java.util.List;

public class MyGenerator {
    public static void main(String [] args) {
        String modelPackage = "com.xgx.distqcp.Domain.Model.Oracle";
        String baseModelPackage = modelPackage+ ".base";

        String modelDir = PathKit.getWebRootPath() + "/src/main/java/" + modelPackage.replace(".", "/");
        String baseModelDir = PathKit.getWebRootPath() + "/src/main/java/" + baseModelPackage.replace(".", "/");
        MyMetaBuilder m=new MyMetaBuilder(getDataSource());
        m.setDialect(new OracleDialect());
        List<TableMeta> tableMetaList = m.build();
        new JbootBaseModelGenerator(baseModelPackage, baseModelDir).generate(tableMetaList);
        new JbootModelnfoGenerator(modelPackage, baseModelPackage, modelDir).generate(tableMetaList);

    }
    public static DataSource getDataSource(){
        DatasourceConfig datasourceConfig = JbootProperties.get(DatasourceConfig.class, "jboot.datasource.oracle");
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(datasourceConfig.getUrl());
        config.setUsername(datasourceConfig.getUser());
        config.setPassword(datasourceConfig.getPassword());
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.setDriverClassName(datasourceConfig.getDriverClassName());

        return new HikariDataSource(config);
    }
}
